/**
 * 
 */


function MapEditor(){
	// 地图
	this.map = null;
	// 路径层的数据
	this.paths = [];  // 越靠后的越在顶层
	this.visibeOfPathLayer = true;  // true表示显示
	
	// 背景层的数据
	this.backgrounds = [];
	this.visibeOfBackgroundLayer = true; // true表示显示
	
	// 标注
	this.markers = [];
	
	// 网格
	this.grids = [];
	
	// 轨迹
	this.foottraces = []; // 
	
	
	// id计数器
	this.maxId = 0;
}

/**
 * 初始化 
 */
MapEditor.prototype.init = function (map){
	this.map = map;
}

/**
 * setter 参数为bool
 */
MapEditor.prototype.setVisibleOfGrids = function (v){
	
	
	var len = this.grids.length;
	for(var i=len-1;i>=0;i--){
		var one = this.grids[i];
		if(v){
			one.show();
		}else{
			one.hide();
		}
	}
}

/**
 * setter 参数为bool
 */
MapEditor.prototype.setVisibleOfMarker = function (v){
	
	
	var len = this.markers.length;
	for(var i=len-1;i>=0;i--){
		var one = this.markers[i];
		if(v){
			one.show();
		}else{
			one.hide();
		}
	}
}
/**
 * setter 参数为bool
 */
MapEditor.prototype.setVisibleOfMarkerName = function (v){
	
	
	var len = this.markers.length;
	for(var i=len-1;i>=0;i--){
		var one = this.markers[i];
		var ext = one.getExtData();
		if(v){
			// 设置label标签
			one.setLabel({//label的父div默认蓝框白底右下角显示，样式className为：amap-marker-label
		        offset:new AMap.Pixel(0,0),//修改父div相对于maker的位置
		        content:ext.label
		    });
		}else{
			// 设置label标签
			one.setLabel({//label的父div默认蓝框白底右下角显示，样式className为：amap-marker-label
		        offset:new AMap.Pixel(100000,100000),//修改父div相对于maker的位置
		        content:ext.label
		    });
		}
	}
}

/**
 * setter 参数为bool
 */
MapEditor.prototype.setVisibleOfPathLayer = function (v){
	// 取消选中
	this.selectAllPath(false);
	
	this.visibeOfPathLayer = v;
	
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		ext.setVisible(this.visibeOfPathLayer);
		if(ext.getVisible()){
			one.show();
		}else{
			one.hide();
		}
	}
}
/**
 * setter  参数为bool
 */
MapEditor.prototype.setVisibleOfBackgroundLayer = function (v){
	// 取消选中
	this.selectAllBackground(false);
	
	this.visibeOfBackgroundLayer = v;
	
	
	
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		ext.setVisible(this.visibeOfBackgroundLayer);
		if(ext.getVisible()){
			one.show();
		}else{
			one.hide();
		}
	}
}
/**
 * 添加grid
 */
MapEditor.prototype.addGrid = function (obj){
	this.grids.push(obj);
}
/**
 * 添加marker
 */
MapEditor.prototype.addMarker = function (obj){
	this.markers.push(obj);
}
/**
 * 添加路径数据 
 */
MapEditor.prototype.addPathLayerChild = function (obj){
	// 排除重复
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		if(one==obj){
			return;
		}
	}
	// 设置zIndex
	var option = obj.getOptions( );
	option.zIndex = this.paths.length;
	obj.setOptions(option);
	
	// 新建
	var ext = new MapObjExtData();
	ext.setSelect(false);
	ext.setId(this.maxId++);
	ext.setType(MapCommon.prototype.OBJECT_TYPE_11);
	obj.setExtData(ext);
	this.paths.push(obj);
}
/**
 * 添加背景数据 
 */
MapEditor.prototype.addBackgroundLayerChild = function (obj){
	// 排除重复
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		if(one==obj){
			return;
		}
	}
	// 设置zIndex
	var option = obj.getOptions( );
	option.zIndex = this.backgrounds.length;
	obj.setOptions(option);
	
	
	// 新建
	var ext = new MapObjExtData();
	ext.setSelect(false);
	ext.setId(this.maxId++);
	ext.setType(MapCommon.prototype.OBJECT_TYPE_1);
	obj.setExtData(ext);
	this.backgrounds.push(obj);
}
/**
 * 删除道路数据 
 */
MapEditor.prototype.deleteSelectPath = function(){
	// 修改内核数据
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			this.paths.splice(i, 1);
			// 从界面删除
			one.setMap(null);
		}
	}
}
/**
 * 删除背景数据 
 */
MapEditor.prototype.deleteSelectBackground = function(){
	// 修改内核数据
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			this.backgrounds.splice(i, 1);
			// 从界面删除
			one.setMap(null);
		}
	}
}
/**
 * 移动层次
 * @param isUp
 */
MapEditor.prototype.moveUp = function(isUp, isPath){
	var target = isPath?this.paths:this.backgrounds;
	
	var len = target.length;
	for(var i=len-1;i>=0;i--){
		var one = target[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			// 如果isUp为true，则上方的zIndex减少，当前zIndex增加
			// 如果isUp为false，则下方的zIndex增加，当前zIndex减少
			if(isUp){
				if(i<len-1){
					// 交换zIndex
					var u = target[i+1];
					var up = u.getOptions();
					var tmp = up.zIndex;
					var cur = one.getOptions( );
					up.zIndex = cur.zIndex;
					cur.zIndex = tmp;
					one.setOptions(cur);
					u.setOptions(up);
					
					// 交换数据
					target.splice(i, 1);
					target.splice(i+1,0, one);
				}
			}else{
				if(i>0){
					// 交换zIndex
					var d = target[i-1];
					var cur = one.getOptions( );
					var tmp = cur.zIndex;
					var down = d.getOptions();
					cur.zIndex = down.zIndex;
					down.zIndex = tmp;
					one.setOptions(cur);
					d.setOptions(down);
					
					// 交换数据
					target.splice(i, 1);
					target.splice(i-1,0, one);
					
					i--;// 跳过自己
				}
			}
			
			
			
		}
	}
}


/**
 * 刷新颜色
 */
MapEditor.prototype.refreshColor = function(){
	// 路线层
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		var option = one.getOptions(); 
		if(!ext.getSelect()){
			option.strokeStyle 	= MapColor.prototype.BASE_STROKE_SOLID;	
			option.strokeDasharray = [0,0,0];
		}else{
			
			option.strokeStyle 	= MapColor.prototype.BASE_STROKE_DASHED;	
			option.strokeDasharray = [10,10]; 
		}
		option.strokeColor = MapCommon.prototype.COLOR_MAP[ext.getType()].strokeColor;
		option.strokeOpacity = MapCommon.prototype.COLOR_MAP[ext.getType()].strokeOpacity;
		
		one.setOptions(option);
	}
	
	// 背景层
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		var option = one.getOptions(); 
		if(!ext.getSelect()){
			option.strokeStyle 	= MapColor.prototype.BASE_STROKE_SOLID;	
			option.strokeDasharray = [0,0,0];
		}else{
			
			option.strokeStyle 	= MapColor.prototype.BASE_STROKE_DASHED;	
			option.strokeDasharray = [10,10]; 
		}
		option.strokeColor = MapCommon.prototype.COLOR_MAP[ext.getType()].strokeColor;
		option.strokeOpacity = MapCommon.prototype.COLOR_MAP[ext.getType()].strokeOpacity;
		option.fillColor = MapCommon.prototype.COLOR_MAP[ext.getType()].fillColor;
		option.fillOpacity = MapCommon.prototype.COLOR_MAP[ext.getType()].fillOpacity;
		
		one.setOptions(option);
	}
}
/**
 * 切换选中 
 */
MapEditor.prototype.reverseSelectState = function(one){
	// 修改内核数据
	var ext = one.getExtData();
	ext.setSelect(!ext.getSelect());
	one.setExtData(ext);	
	
	// 修改界面
	var option = one.getOptions(); 
	if(!ext.getSelect()){
		option.strokeStyle 	= MapColor.prototype.BASE_STROKE_SOLID;	
		option.strokeDasharray = [0,0,0];
	}else{
		
		option.strokeStyle 	= MapColor.prototype.BASE_STROKE_DASHED;	
		option.strokeDasharray = [10,10]; 
	}
	
	one.setOptions(option);
	
}

/**
 * 选中 单个
 * one:被选中对象
 * select:true表示选中
 */
MapEditor.prototype.selectOne = function(one, select){
	// 修改内核数据
	var ext = one.getExtData();
	ext.setSelect(select);
	one.setExtData(ext);

	// 修改界面
	var option = one.getOptions(); 
	if(!ext.getSelect()){
		option.strokeStyle 	= MapColor.prototype.BASE_STROKE_SOLID;	
		option.strokeDasharray = [0,0,0];
	}else{
		
		option.strokeStyle 	= MapColor.prototype.BASE_STROKE_DASHED;	
		option.strokeDasharray = [10,10]; 
	}
	
	one.setOptions(option);
}


/**
 * 选中 所有
 */
MapEditor.prototype.selectAllPath  = function(select){

	// 修改内核数据
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		ext.setSelect(select);
		one.setExtData(ext);
		this.selectOne(one, ext.getSelect());
	}
}

MapEditor.prototype.selectAllBackground= function(select){
	// 修改内核数据
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		ext.setSelect(select);
		one.setExtData(ext);
		this.selectOne(one, ext.getSelect());
	}
}

/**
 * 更新
 */
MapEditor.prototype.updatePathLayerChild  = function(){
	
}
MapEditor.prototype.updateBackgroundLayerChild  = function(){
	
}

/**
 * 取得选中的
 */
MapEditor.prototype.getSelectPath  = function(){
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			return one;
		}
	}
	return null;
}
MapEditor.prototype.getSelectBackground  = function(){
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			return one;
		}
	}
	return null;
}
/**
 * 取得所有选中的
 */
MapEditor.prototype.getAllSelectPath  = function(){
	var ret = [];
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			ret.push(one);
		}
	}
	return ret;
}
MapEditor.prototype.getAllSelectBackground  = function(){
	var ret = [];
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		var ext = one.getExtData();
		if(ext.getSelect()){
			ret.push(one);
		}
	}
	return ret;
}


/**
 * 序列化／反序列化 
 * 格式如：
 * 			{
 * 				version:1,
 * 				data:{
 * 					paths:[
 * 						{
 * 							points:[],	// 二维数组表示多个路径，每个路径是个一维数组，经纬度交错排列，经度在前
 * 							ext:{}		// MapObjExtData (type,hard)
 * 						}
 * 					],
 * 					backgrounds:[
 * 						{
 * 							points:[],	// 二维数组表示多个路径，每个路径是个一维数组，经纬度交错排列，经度在前
 * 							ext:{}		// MapObjExtData (type,hard)
 * 						}
 * 					]
 * 				}
 * 			}
 */
MapEditor.prototype.toJson = function (){
	var ret = "{";
	ret +=('"version":'+MapCommon.prototype.LUTU_MAP_VERSION_1);
	ret +=(',"data":{');
	
	// paths:
	ret +=('"paths":[');
	var len = this.paths.length;
	for(var i=0;i<len;i++){
		if(i>0){
			ret +=",";
		}
		var one = this.paths[i];
		var json = MapUtil.prototype.mapObjToJson(one);
		ret += json;
	}
	ret += "]";
	
	// backgrounds:
	ret +=(',"backgrounds":[');
	var len = this.backgrounds.length;
	for(var i=0;i<len;i++){
		if(i>0){
			ret +=",";
		}
		var one = this.backgrounds[i];
		var json = MapUtil.prototype.mapObjToJson(one);
		ret += json;
	}
	ret += "]";
	
	
	
	ret += "}";
	ret += "}";
	
	return ret;
}
MapEditor.prototype.fromJson = function (jsonObj){
	this.deleteAll();// 删除所有
	
	if(jsonObj.version != MapCommon.prototype.LUTU_MAP_VERSION_1){
		alert("没有此版本的解析函数:"+jsonObj.version);
		return;
	}
	
	this.version = jsonObj.version;
	// paths:
	var len = jsonObj.data.paths.length;
	for(i=0;i<len;i++){
		var one = jsonObj.data.paths[i];
		var path = MapUtil.prototype.mapObjFromJson(one);
		var options = path.getOptions();
		options.zIndex = i;
		path.setOptions(options);
		this.paths.push(path);
	}
	// backgrounds:
	var len = jsonObj.data.backgrounds.length;
	for(i=0;i<len;i++){
		var one = jsonObj.data.backgrounds[i];
		var path = MapUtil.prototype.mapObjFromJson(one);
		var options = path.getOptions();
		options.zIndex = i;
		path.setOptions(options);

		this.backgrounds.push(path);
	}
}
MapEditor.prototype.footTracesFromJsonStr = function(jsonStrOfList){
	if(jsonStrOfList==null || jsonStrOfList.length==0){
		return;
	}
	for(var m=0;m<jsonStrOfList.length; m++){
		var jsonObj = JSON.parse(jsonStrOfList[m].trace);
		if(jsonObj.version != MapCommon.prototype.LUTU_MAP_VERSION_1){
			alert("没有此版本的解析函数:"+jsonObj.version);
			return;
		}
		
		// paths:
		var len = jsonObj.data.paths.length;
		for(var i=0;i<len;i++){
			var one = jsonObj.data.paths[i];
			var path = MapUtil.prototype.mapObjFromJson(one);
			var options = path.getOptions();
			options.zIndex = this.foottraces.length+1000;
			path.setOptions(options);
			this.foottraces.push(path);
		}
	}
}
/**
 * 描绘行走痕迹
 */
MapEditor.prototype.drawFootTraces = function (v){
	
	
	// 建立新数据
	var len = this.foottraces.length;
	for(i=0;i<len;i++){
		var one = this.foottraces[i];
		one.setMap(this.map);

		// 显示或隐藏
		var ext = one.getExtData();
		ext.setVisible(v);
		if(ext.getVisible()){
			one.show();
		}else{
			one.hide();
		}
	}
}

/**
 * setter 参数为bool
 */
MapEditor.prototype.setVisibleOfFootTraces = function (v){
	var len = this.foottraces.length;
	for(var i=len-1;i>=0;i--){
		var one = this.foottraces[i];
		var ext = one.getExtData();
		ext.setVisible(v);
		if(ext.getVisible()){
			one.show();
		}else{
			one.hide();
		}
	}

}



/**
 * 删除数据
 */
MapEditor.prototype.deleteAll = function(){
	// 删除旧数据
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		one.setMap(null);
		this.paths[i] = null;
	}
	this.paths.splice(0, len);
	
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		one.setMap(null); 
		this.backgrounds[i] = null;
	}
	this.backgrounds.splice(0, len);
}
/**
 * 描绘 
 */
MapEditor.prototype.draw = function (){
	
	
	// 建立新数据
	var len = this.paths.length;
	for(i=0;i<len;i++){
		var one = this.paths[i];
		one.setMap(this.map);
	}
	var len = this.backgrounds.length;
	for(i=0;i<len;i++){
		var one = this.backgrounds[i];
		one.setMap(this.map);
	}
}

/**
 * 
 */
MapEditor.prototype.enableClick = function(callback){	
	var len = this.paths.length;
	for(var i=len-1;i>=0;i--){
		var one = this.paths[i];
		//AMap.event.removeListener(one,callback);
		AMap.event.addListener(one, 'click', callback);
	}
	var len = this.backgrounds.length;
	for(var i=len-1;i>=0;i--){
		var one = this.backgrounds[i];
		//AMap.event.removeListener(one,callback);
		AMap.event.addListener(one, 'click', callback);
	}
	
}
